#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：13.使用上下文管理器操作互斥锁.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-01 10:05
'''
import threading
from threading import RLock

num = 0
# 在全局中创建互斥锁对象
lock = RLock()

def add():
    global num
    for i in range(10):
        with lock:
            num += i
        print(f"加法的结果：{num}")


def sub():
    global num
    for i in range(10):
        with lock:
            num -= i
        print(f"减法的结果：{num}")

t1 = threading.Thread(target=add)
t2 = threading.Thread(target=sub)

t1.start()
t2.start()

t1.join()
t2.join()

print(f"最终结果：{num}")